using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using GotDotNet.SharePointExtractor;
using System.Collections.Generic;
using System.Text;
using SharePoint.ImportExport.Core;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace cmdSetSitesReadOnly
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 6)
            {
                Console.WriteLine("Usage {0} -path [Path To Xml] -readonly true/false -VirtualServer url", System.AppDomain.CurrentDomain.FriendlyName);
                return;
            }
            if (File.Exists(args[1])) //Multiple sites specified
            {
                // A FileStream is needed to read the XML document.
                FileStream fs = new FileStream(args[1], FileMode.Open);
                XmlReader reader = new XmlTextReader(fs);
                // Declare an object variable of the type to be deserialized.
                GotDotNet.SharePointExtractor.SitesList sites = new SitesList();
                // Create an instance of the XmlSerializer specifying type and namespace.
                XmlSerializer serializer = new XmlSerializer(typeof(SitesList));

                try
                {
                    // Use the Deserialize method to restore the object's state.
                    sites = (SitesList)serializer.Deserialize(reader);
                }
                catch (InvalidOperationException ex)
                {
                    Logger.Log(ErrorLevel.Error, "Error loading " + args[1] + ": " + ex.Message + "\t" + ex.InnerException.Message);
                    return ;
                }
                finally
                {
                    reader.Close();
                    fs.Close();
                }

                SPGlobalAdmin globalAdmin = new SPGlobalAdmin();

                System.Uri uri = new System.Uri(args[5]);

                SPVirtualServer virtualServer = globalAdmin.OpenVirtualServer(uri);
                


                foreach (GotDotNet.SharePointExtractor.SiteToBeExported siteToProcess in sites.SiteList)
                {
                    SPSite siteCollection = virtualServer.Sites[siteToProcess.Url];
                    {
                        bool writeOnly = bool.Parse(args[3]);
                        if (writeOnly)
                        {
                            siteCollection.WriteLocked = true;
                            siteCollection.LockIssue = "Site is being migrated";
                        }
                        else
                        {
                            siteCollection.WriteLocked = false;
                        }
                    }
                }
            }

        }
    }
}
